#!/bin/bash

# 最终修复脚本

set -e

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'

log_info() {
    echo -e "${BLUE}[INFO]${NC} $1"
}

log_success() {
    echo -e "${GREEN}[SUCCESS]${NC} $1"
}

log_warning() {
    echo -e "${YELLOW}[WARNING]${NC} $1"
}

log_error() {
    echo -e "${RED}[ERROR]${NC} $1"
}

PROJECT_DIR=$(dirname "$(readlink -f "$0")")

log_info "开始最终修复..."

# 确保在项目根目录
cd "$PROJECT_DIR"
log_info "当前目录: $(pwd)"

# 激活虚拟环境
if [ -d "yxyweb_venv" ]; then
    source yxyweb_venv/bin/activate
    log_info "已激活虚拟环境"
else
    log_error "虚拟环境不存在，请先运行 ./install.sh"
    exit 1
fi

# 设置Python路径
export PYTHONPATH="$PROJECT_DIR:$PYTHONPATH"
log_info "已设置Python路径: $PYTHONPATH"

# 测试修复
log_info "运行简单测试..."
python3 test_simple.py

if [ $? -eq 0 ]; then
    log_success "测试通过！现在可以启动服务了。"
    echo
    echo "运行以下命令启动服务："
    echo "  ./start.sh"
else
    log_error "测试失败，请检查错误信息"
    exit 1
fi
